package cn.ronghuanet.org.controller;

import cn.ronghuanet.base.annotation.RonghuaPermission;
import cn.ronghuanet.base.util.AjaxResult;
import cn.ronghuanet.base.util.PageInfo;
import cn.ronghuanet.org.domain.Department;
import cn.ronghuanet.org.query.DepartmentQuery;
import cn.ronghuanet.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

// @Controller
@RestController // 以后我们后台只会返回数据 所以我们使用RestController
@RequestMapping("/department")
@Api(value = "部门管理接口",description = "部门的管理")
@RonghuaPermission(name = "部门管理",desc = "部门相关接口")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

     /*
        /department 新增和修改  put
        /department/{id} 删除  delete
        /department/{id} 查询一个 get
        /department 查询全部 get
        /department 删除多个 delete
        叫做 RestFul的请求风格 不在通过路径控制访问某个接口
        而是通过 请求方式
     */
    /*
        post 和put 请求 参数都是在body里面的 直接取值 取不到
        需要使用 注解 RequestBody 告诉代码从body中取值
     */
    @ApiOperation(value = "这个是新增和修改接口")
    @PutMapping
    @RonghuaPermission(name = "部门保存",desc = "部门新增和修改")
    public AjaxResult save(@RequestBody  Department department){
        try {
            if (Objects.isNull(department.getId())){
                departmentService.insert(department);
            }else{
                departmentService.update(department);
            }
           return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    @ApiOperation(value = "这个是根据id删除接口")
    @DeleteMapping("/{id}")
    @RonghuaPermission(name = "部门删除",desc = "根据id删除某个部门")
    public AjaxResult deleteById(@PathVariable("id")Long id){
        try {
            departmentService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    @RonghuaPermission(name = "部门删除",desc = "批量删除部门")
    @PatchMapping
    public AjaxResult delete(@RequestBody List<String> ids){
        try {
            departmentService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    @GetMapping
    @RonghuaPermission(name = "部门查询",desc = "查询所有部门")
    public AjaxResult selectAll(){
        try {
            List<Department> departments = departmentService.selectAll();
            return AjaxResult.success(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    @RonghuaPermission(name = "部门查询",desc = "根据id查询部门")
    @GetMapping("/{id}")
    public AjaxResult selectById(@PathVariable("id")String id){
        try {
            Department department = departmentService.selectById(id);
            return AjaxResult.success(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    @GetMapping("/deptTree")
    @RonghuaPermission(name = "部门查询",desc = "查询部门树")
    public AjaxResult deptTree(){
        try {
            List<Department> deptTree = departmentService.deptTree();
            return AjaxResult.success(deptTree);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    /*
        所有的接口 返回值 要做统一处理 不能仅仅返回一个 true或者是false 或者是对象
        如果是这样前端不好判断
        我可以给前端返回一个对象
        success msg data
        Map 可以 但是麻烦
        设置一个全局的 AjaxResult 对象
     */

    /**
     * 分页查询接口
     */
    @PostMapping
    @RonghuaPermission(name = "部门查询",desc = "部门分页查询")
    public AjaxResult queryPage(@RequestBody DepartmentQuery departmentQuery){
        try {
           PageInfo pageInfo = departmentService.queryPage(departmentQuery);
           return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

    }

}
